home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / sunaudiodev.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  10.9 KB  |  519 lines

  1. /* Sad objects */
  2.  
  3. #include "Python.h"
  4. #include "structmember.h"
  5.  
  6. #ifdef HAVE_SYS_AUDIOIO_H
  7. #define SOLARIS
  8. #endif
  9.  
  10. #ifdef HAVE_UNISTD_H
  11. #include <unistd.h>
  12. #endif
  13.  
  14. #ifdef HAVE_FCNTL_H
  15. #include <fcntl.h>
  16. #endif
  17.  
  18. #include <stropts.h>
  19. #include <sys/ioctl.h>
  20. #ifdef SOLARIS
  21. #include <sys/audioio.h>
  22. #else
  23. #include <sun/audioio.h>
  24. #endif
  25.  
  26. /* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */
  27.  
  28. typedef struct {
  29.     PyObject_HEAD
  30.     int    x_fd;        /* The open file */
  31.     int    x_icount;    /* # samples read */
  32.     int    x_ocount;    /* # samples written */
  33.     int    x_isctl;    /* True if control device */
  34.     
  35. } sadobject;
  36.  
  37. typedef struct {
  38.     PyObject_HEAD
  39.     audio_info_t ai;
  40. } sadstatusobject;
  41.  
  42. staticforward PyTypeObject Sadtype;
  43. staticforward PyTypeObject Sadstatustype;
  44. static sadstatusobject *sads_alloc();    /* Forward */
  45.  
  46. static PyObject *SunAudioError;
  47.  
  48. #define is_sadobject(v)        ((v)->ob_type == &Sadtype)
  49. #define is_sadstatusobject(v)    ((v)->ob_type == &Sadstatustype)
  50.  
  51.  
  52. static sadobject *
  53. newsadobject(arg)
  54.     PyObject *arg;
  55. {
  56.     sadobject *xp;
  57.     int fd;
  58.     char *mode;
  59.     int imode;
  60.     char* basedev;
  61.     char* ctldev;
  62.     char* opendev;
  63.  
  64.     /* Check arg for r/w/rw */
  65.     if (!PyArg_Parse(arg, "s", &mode))
  66.         return NULL;
  67.     if (strcmp(mode, "r") == 0)
  68.         imode = 0;
  69.     else if (strcmp(mode, "w") == 0)
  70.         imode = 1;
  71.     else if (strcmp(mode, "rw") == 0)
  72.         imode = 2;
  73.     else if (strcmp(mode, "control") == 0)
  74.         imode = -1;
  75.     else {
  76.         PyErr_SetString(SunAudioError,
  77.               "Mode should be one of 'r', 'w', 'rw' or 'control'");
  78.         return NULL;
  79.     }
  80.     
  81.     /* Open the correct device.  The base device name comes from the
  82.      * AUDIODEV environment variable first, then /dev/audio.  The
  83.      * control device tacks "ctl" onto the base device name.
  84.      */
  85.     basedev = getenv("AUDIODEV");
  86.     if (!basedev)
  87.         basedev = "/dev/audio";
  88.     ctldev = PyMem_NEW(char, strlen(basedev) + 4);
  89.     if (!ctldev) {
  90.         PyErr_NoMemory();
  91.         return NULL;
  92.     }
  93.     strcpy(ctldev, basedev);
  94.     strcat(ctldev, "ctl");
  95.  
  96.     if (imode < 0) {
  97.         opendev = ctldev;
  98.         fd = open(ctldev, 2);
  99.     }
  100.     else {
  101.         opendev = basedev;
  102.         fd = open(basedev, imode);
  103.     }
  104.     if (fd < 0) {
  105.         PyErr_SetFromErrnoWithFilename(SunAudioError, opendev);
  106.         return NULL;
  107.     }
  108.     PyMem_DEL(ctldev);
  109.  
  110.     /* Create and initialize the object */
  111.     xp = PyObject_New(sadobject, &Sadtype);
  112.     if (xp == NULL) {
  113.         close(fd);
  114.         return NULL;
  115.     }
  116.     xp->x_fd = fd;
  117.     xp->x_icount = xp->x_ocount = 0;
  118.     xp->x_isctl = (imode < 0);
  119.     
  120.     return xp;
  121. }
  122.  
  123. /* Sad methods */
  124.  
  125. static void
  126. sad_dealloc(xp)
  127.     sadobject *xp;
  128. {
  129.         close(xp->x_fd);
  130.     PyObject_Del(xp);
  131. }
  132.  
  133. static PyObject *
  134. sad_read(self, args)
  135.         sadobject *self;
  136.         PyObject *args;
  137. {
  138.         int size, count;
  139.     char *cp;
  140.     PyObject *rv;
  141.     
  142.         if (!PyArg_Parse(args, "i", &size))
  143.         return NULL;
  144.     rv = PyString_FromStringAndSize(NULL, size);
  145.     if (rv == NULL)
  146.         return NULL;
  147.  
  148.     if (!(cp = PyString_AsString(rv)))
  149.         goto finally;
  150.  
  151.     count = read(self->x_fd, cp, size);
  152.     if (count < 0) {
  153.         PyErr_SetFromErrno(SunAudioError);
  154.         goto finally;
  155.     }
  156. #if 0
  157.     /* TBD: why print this message if you can handle the condition?
  158.      * assume it's debugging info which we can just as well get rid
  159.      * of.  in any case this message should *not* be using printf!
  160.      */
  161.     if (count != size)
  162.         printf("sunaudio: funny read rv %d wtd %d\n", count, size);
  163. #endif
  164.     self->x_icount += count;
  165.     return rv;
  166.  
  167.   finally:
  168.     Py_DECREF(rv);
  169.     return NULL;
  170. }
  171.  
  172. static PyObject *
  173. sad_write(self, args)
  174.         sadobject *self;
  175.         PyObject *args;
  176. {
  177.         char *cp;
  178.     int count, size;
  179.     
  180.         if (!PyArg_Parse(args, "s#", &cp, &size))
  181.         return NULL;
  182.  
  183.     count = write(self->x_fd, cp, size);
  184.     if (count < 0) {
  185.         PyErr_SetFromErrno(SunAudioError);
  186.         return NULL;
  187.     }
  188. #if 0
  189.     if (count != size)
  190.         printf("sunaudio: funny write rv %d wanted %d\n", count, size);
  191. #endif
  192.     self->x_ocount += count;
  193.     
  194.     Py_INCREF(Py_None);
  195.     return Py_None;
  196. }
  197.  
  198. static PyObject *
  199. sad_getinfo(self, args)
  200.     sadobject *self;
  201.     PyObject *args;
  202. {
  203.     sadstatusobject *rv;
  204.  
  205.     if (!PyArg_Parse(args, ""))
  206.         return NULL;
  207.     if (!(rv = sads_alloc()))
  208.         return NULL;
  209.  
  210.     if (ioctl(self->x_fd, AUDIO_GETINFO, &rv->ai) < 0) {
  211.         PyErr_SetFromErrno(SunAudioError);
  212.         Py_DECREF(rv);
  213.         return NULL;
  214.     }
  215.     return (PyObject *)rv;
  216. }
  217.  
  218. static PyObject *
  219. sad_setinfo(self, arg)
  220.     sadobject *self;
  221.     sadstatusobject *arg;
  222. {
  223.     if (!is_sadstatusobject(arg)) {
  224.         PyErr_SetString(PyExc_TypeError,
  225.                 "Must be sun audio status object");
  226.         return NULL;
  227.     }
  228.     if (ioctl(self->x_fd, AUDIO_SETINFO, &arg->ai) < 0) {
  229.         PyErr_SetFromErrno(SunAudioError);
  230.         return NULL;
  231.     }
  232.     Py_INCREF(Py_None);
  233.     return Py_None;
  234. }
  235.  
  236. static PyObject *
  237. sad_ibufcount(self, args)
  238.     sadobject *self;
  239.     PyObject *args;
  240. {
  241.     audio_info_t ai;
  242.     
  243.     if (!PyArg_Parse(args, ""))
  244.         return NULL;
  245.     if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
  246.         PyErr_SetFromErrno(SunAudioError);
  247.         return NULL;
  248.     }
  249.     return PyInt_FromLong(ai.record.samples - self->x_icount);
  250. }
  251.  
  252. static PyObject *
  253. sad_obufcount(self, args)
  254.     sadobject *self;
  255.     PyObject *args;
  256. {
  257.     audio_info_t ai;
  258.     
  259.     if (!PyArg_Parse(args, ""))
  260.         return NULL;
  261.     if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
  262.         PyErr_SetFromErrno(SunAudioError);
  263.         return NULL;
  264.     }
  265.     /* x_ocount is in bytes, wheras play.samples is in frames */
  266.     /* we want frames */
  267.     return PyInt_FromLong(self->x_ocount / (ai.play.channels *
  268.                         ai.play.precision / 8) -
  269.                   ai.play.samples);
  270. }
  271.  
  272. static PyObject *
  273. sad_drain(self, args)
  274.     sadobject *self;
  275.     PyObject *args;
  276. {
  277.     
  278.     if (!PyArg_Parse(args, ""))
  279.         return NULL;
  280.     if (ioctl(self->x_fd, AUDIO_DRAIN, 0) < 0) {
  281.         PyErr_SetFromErrno(SunAudioError);
  282.         return NULL;
  283.     }
  284.     Py_INCREF(Py_None);
  285.     return Py_None;
  286. }
  287.  
  288. #ifdef SOLARIS
  289. static PyObject *
  290. sad_getdev(self, args)
  291.     sadobject *self;
  292.     PyObject *args;
  293. {
  294.     struct audio_device ad;
  295.  
  296.     if (!PyArg_Parse(args, ""))
  297.         return NULL;
  298.     if (ioctl(self->x_fd, AUDIO_GETDEV, &ad) < 0) {
  299.         PyErr_SetFromErrno(SunAudioError);
  300.         return NULL;
  301.     }
  302.     return Py_BuildValue("(sss)", ad.name, ad.version, ad.config);
  303. }
  304. #endif
  305.  
  306. static PyObject *
  307. sad_flush(self, args)
  308.     sadobject *self;
  309.     PyObject *args;
  310. {
  311.     
  312.     if (!PyArg_Parse(args, ""))
  313.         return NULL;
  314.     if (ioctl(self->x_fd, I_FLUSH, FLUSHW) < 0) {
  315.         PyErr_SetFromErrno(SunAudioError);
  316.         return NULL;
  317.     }
  318.     Py_INCREF(Py_None);
  319.     return Py_None;
  320. }
  321.  
  322. static PyObject *
  323. sad_close(self, args)
  324.     sadobject *self;
  325.     PyObject *args;
  326. {
  327.     
  328.     if (!PyArg_Parse(args, ""))
  329.         return NULL;
  330.     if (self->x_fd >= 0) {
  331.         close(self->x_fd);
  332.         self->x_fd = -1;
  333.     }
  334.     Py_INCREF(Py_None);
  335.     return Py_None;
  336. }
  337.  
  338. static PyObject *
  339. sad_fileno(self, args)
  340.     sadobject *self;
  341.     PyObject *args;
  342. {
  343.     if (!PyArg_Parse(args, ""))
  344.         return NULL;
  345.  
  346.     return PyInt_FromLong(self->x_fd);
  347. }
  348.  
  349.  
  350. static PyMethodDef sad_methods[] = {
  351.         { "read",    (PyCFunction)sad_read },
  352.         { "write",    (PyCFunction)sad_write },
  353.         { "ibufcount",    (PyCFunction)sad_ibufcount },
  354.         { "obufcount",    (PyCFunction)sad_obufcount },
  355. #define CTL_METHODS 4
  356.         { "getinfo",    (PyCFunction)sad_getinfo },
  357.         { "setinfo",    (PyCFunction)sad_setinfo },
  358.         { "drain",    (PyCFunction)sad_drain },
  359.         { "flush",    (PyCFunction)sad_flush },
  360. #ifdef SOLARIS
  361.     { "getdev",    (PyCFunction)sad_getdev },
  362. #endif
  363.         { "close",    (PyCFunction)sad_close },
  364.     { "fileno",     (PyCFunction)sad_fileno },
  365.     {NULL,        NULL}        /* sentinel */
  366. };
  367.  
  368. static PyObject *
  369. sad_getattr(xp, name)
  370.     sadobject *xp;
  371.     char *name;
  372. {
  373.     if (xp->x_isctl)
  374.         return Py_FindMethod(sad_methods+CTL_METHODS,
  375.                      (PyObject *)xp, name);
  376.     else
  377.         return Py_FindMethod(sad_methods, (PyObject *)xp, name);
  378. }
  379.  
  380. /* ----------------------------------------------------------------- */
  381.  
  382. static sadstatusobject *
  383. sads_alloc() {
  384.     return PyObject_New(sadstatusobject, &Sadstatustype);
  385. }
  386.  
  387. static void
  388. sads_dealloc(xp)
  389.     sadstatusobject *xp;
  390. {
  391.     PyMem_DEL(xp);
  392. }
  393.  
  394. #define OFF(x) offsetof(audio_info_t,x)
  395. static struct memberlist sads_ml[] = {
  396.     { "i_sample_rate",    T_UINT,        OFF(record.sample_rate) },
  397.     { "i_channels",        T_UINT,        OFF(record.channels) },
  398.     { "i_precision",    T_UINT,        OFF(record.precision) },
  399.     { "i_encoding",        T_UINT,        OFF(record.encoding) },
  400.     { "i_gain",        T_UINT,        OFF(record.gain) },
  401.     { "i_port",        T_UINT,        OFF(record.port) },
  402.     { "i_samples",        T_UINT,        OFF(record.samples) },
  403.     { "i_eof",        T_UINT,        OFF(record.eof) },
  404.     { "i_pause",        T_UBYTE,    OFF(record.pause) },
  405.     { "i_error",        T_UBYTE,    OFF(record.error) },
  406.     { "i_waiting",        T_UBYTE,    OFF(record.waiting) },
  407.     { "i_open",        T_UBYTE,    OFF(record.open) ,     RO},
  408.     { "i_active",        T_UBYTE,    OFF(record.active) ,     RO},
  409. #ifdef SOLARIS
  410.     { "i_buffer_size",    T_UINT,        OFF(record.buffer_size) },
  411.     { "i_balance",        T_UBYTE,    OFF(record.balance) },
  412.     { "i_avail_ports",    T_UINT,        OFF(record.avail_ports) },
  413. #endif
  414.  
  415.     { "o_sample_rate",    T_UINT,        OFF(play.sample_rate) },
  416.     { "o_channels",        T_UINT,        OFF(play.channels) },
  417.     { "o_precision",    T_UINT,        OFF(play.precision) },
  418.     { "o_encoding",        T_UINT,        OFF(play.encoding) },
  419.     { "o_gain",        T_UINT,        OFF(play.gain) },
  420.     { "o_port",        T_UINT,        OFF(play.port) },
  421.     { "o_samples",        T_UINT,        OFF(play.samples) },
  422.     { "o_eof",        T_UINT,        OFF(play.eof) },
  423.     { "o_pause",        T_UBYTE,    OFF(play.pause) },
  424.     { "o_error",        T_UBYTE,    OFF(play.error) },
  425.     { "o_waiting",        T_UBYTE,    OFF(play.waiting) },
  426.     { "o_open",        T_UBYTE,    OFF(play.open) ,     RO},
  427.     { "o_active",        T_UBYTE,    OFF(play.active) ,     RO},
  428. #ifdef SOLARIS
  429.     { "o_buffer_size",    T_UINT,        OFF(play.buffer_size) },
  430.     { "o_balance",        T_UBYTE,    OFF(play.balance) },
  431.     { "o_avail_ports",    T_UINT,        OFF(play.avail_ports) },
  432. #endif
  433.  
  434.     { "monitor_gain",    T_UINT,        OFF(monitor_gain) },
  435.         { NULL,                 0,              0},
  436. };
  437.  
  438. static PyObject *
  439. sads_getattr(xp, name)
  440.     sadstatusobject *xp;
  441.     char *name;
  442. {
  443.     return PyMember_Get((char *)&xp->ai, sads_ml, name);
  444. }
  445.  
  446. static int
  447. sads_setattr(xp, name, v)
  448.     sadstatusobject *xp;
  449.     char *name;
  450.     PyObject *v;
  451. {
  452.  
  453.     if (v == NULL) {
  454.         PyErr_SetString(PyExc_TypeError,
  455.                 "can't delete sun audio status attributes");
  456.         return -1;
  457.     }
  458.     return PyMember_Set((char *)&xp->ai, sads_ml, name, v);
  459. }
  460.  
  461. /* ------------------------------------------------------------------- */
  462.  
  463.  
  464. static PyTypeObject Sadtype = {
  465.     PyObject_HEAD_INIT(&PyType_Type)
  466.     0,                /*ob_size*/
  467.     "sun_audio_device",        /*tp_name*/
  468.     sizeof(sadobject),        /*tp_size*/
  469.     0,                /*tp_itemsize*/
  470.     /* methods */
  471.     (destructor)sad_dealloc,    /*tp_dealloc*/
  472.     0,                /*tp_print*/
  473.     (getattrfunc)sad_getattr,    /*tp_getattr*/
  474.     0,                /*tp_setattr*/
  475.     0,                /*tp_compare*/
  476.     0,                /*tp_repr*/
  477. };
  478.  
  479. static PyTypeObject Sadstatustype = {
  480.     PyObject_HEAD_INIT(&PyType_Type)
  481.     0,                /*ob_size*/
  482.     "sun_audio_device_status",    /*tp_name*/
  483.     sizeof(sadstatusobject),    /*tp_size*/
  484.     0,                /*tp_itemsize*/
  485.     /* methods */
  486.     (destructor)sads_dealloc,    /*tp_dealloc*/
  487.     0,                /*tp_print*/
  488.     (getattrfunc)sads_getattr,    /*tp_getattr*/
  489.     (setattrfunc)sads_setattr,    /*tp_setattr*/
  490.     0,                /*tp_compare*/
  491.     0,                /*tp_repr*/
  492. };
  493. /* ------------------------------------------------------------------- */
  494.  
  495. static PyObject *
  496. sadopen(self, args)
  497.     PyObject *self;
  498.     PyObject *args;
  499. {
  500.     return (PyObject *)newsadobject(args);
  501. }
  502.     
  503. static PyMethodDef sunaudiodev_methods[] = {
  504.     { "open", sadopen },
  505.     { 0, 0 },
  506. };
  507.  
  508. void
  509. initsunaudiodev()
  510. {
  511.     PyObject *m, *d;
  512.  
  513.     m = Py_InitModule("sunaudiodev", sunaudiodev_methods);
  514.     d = PyModule_GetDict(m);
  515.     SunAudioError = PyErr_NewException("sunaudiodev.error", NULL, NULL);
  516.     if (SunAudioError)
  517.         PyDict_SetItemString(d, "error", SunAudioError);
  518. }
  519.